package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"srv/model/mysql"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodUpdateLogic {
	return &GoodUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodUpdateLogic) GoodUpdate(in *srv.GoodUpdateRequest) (*srv.GoodUpdateResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Good{}
	err := g.GoodInfo(int(in.UserId))
	if err != nil {
		return nil, fmt.Errorf("商品不存在")
	}
	gg := mysql.Good{
		Model:     gorm.Model{ID: uint(in.UserId)},
		GoodName:  g.GoodName,
		GoodNum:   g.GoodNum,
		GoodPrice: g.GoodPrice,
		GoodStock: g.GoodStock,
		GoodLike:  g.GoodLike,
		GoodAdopt: int(in.GoodAdopt),
	}

	return &srv.GoodUpdateResponse{
		GoodId:    int64(gg.ID),
		GoodName:  gg.GoodName,
		GoodNum:   gg.GoodNum,
		GoodPrice: float32(gg.GoodPrice),
		GoodStock: gg.GoodStock,
		GoodLike:  gg.GoodLike,
		GoodAdopt: int64(gg.GoodAdopt),
	}, nil
}
